package com.freesky.biz.icbc.controller;

import com.freesky.biz.icbc.domain.TScanBusi;
import com.freesky.biz.icbc.domain.TScanReceive;
import com.freesky.biz.icbc.domain.TScanUser;
import com.freesky.biz.icbc.service.IScanBusiService;
import com.freesky.biz.icbc.service.IScanChannelService;
import com.freesky.biz.icbc.service.IScanUserService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.annotation.Log;
import com.freesky.common.annotation.RepeatSubmit;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.enums.BusinessType;
import com.freesky.common.exception.CustomException;
import com.freesky.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 线下扫码业务(人员管理）
 */
@RestController
@RequestMapping("/icbc/scanuser")
public class ScanUserController extends BaseController {
    @Autowired
    private IScanUserService scanUserService;

    @RepeatSubmit
    @Log(title = "扫码人员", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('icbc:scanuser:export')")
    @GetMapping("/export")
    public AjaxResult export(TScanUser scanUser,
                             HttpServletResponse response) {
        return scanUserService.exportFile(scanUser);
    }

    @PreAuthorize("@ss.hasPermi('icbc:scanuser:import')")
    @Log(title = "扫码人员", businessType = BusinessType.IMPORT)
    @RepeatSubmit
    @RequestMapping("/import")
    public AjaxResult importExcel(MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new CustomException("导入文件不能为空！");
        }
        return scanUserService.importExcel(file);
    }

    /**
     * 获取扫码人员列表
     */
    @PreAuthorize("@ss.hasPermi('icbc:scanuser:list')")
    @GetMapping("/list")
    public TableDataInfo list(TScanUser scanUser) {
        startPage();
        List<TScanUser> list = scanUserService.selectScanUserList(scanUser);
        return getDataTable(list);
    }

    /**
     * 根据扫码人员获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('icbc:scanuser:query')")
    @GetMapping(value = "/{scanUserId}")
    public AjaxResult getInfo(@PathVariable Long scanUserId) {
        return success(scanUserService.selectScanUserById(scanUserId));
    }

    /**
     * 新增扫码人员
     */
    @PreAuthorize("@ss.hasPermi('icbc:scanuser:add')")
    @Log(title = "扫码人员管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TScanUser scanUser) {

        if(StringUtils.isEmpty(scanUser.getMobile())){
            return error("新增人员失败，手机号码为空值！");
        }
        scanUser.setScanUserId(SeqUtil.getSeqId());
        if (!scanUserService.checkMobileUnique(scanUser))
        {
            return error("新增人员'" + scanUser.getName() + "'失败，手机号码已存在");
        }

        return toAjax(scanUserService.insertScanUser(scanUser));
    }

    /**
     * 修改扫码人员
     */
    @PreAuthorize("@ss.hasPermi('icbc:scanuser:edit')")
    @Log(title = "扫码人员管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TScanUser scanUser) {
        if(StringUtils.isEmpty(scanUser.getMobile())){
            return error("修改人员失败，手机号码为空值！");
        }

        if (!scanUserService.checkMobileUnique(scanUser)) {
            return error("修改人员'" + scanUser.getName() + "'失败，手机号码已存在");
        }

        TScanUser scanQryUser = scanUserService.selectScanUserById(Long.parseLong(scanUser.getScanUserId()));
        if(scanQryUser!=null){
            long totalScore = Long.parseLong(scanUser.getTotalScore());
            long usedScore = Long.parseLong(scanQryUser.getUsedScore());
            if(totalScore<usedScore){
                return error("修改人员'" + scanUser.getName() + "'失败，分配积分不能少于已使用积分");
            }
        }
        return toAjax(scanUserService.updateScanUser(scanUser));
    }

    /**
     * 删除扫码人员
     */
    @PreAuthorize("@ss.hasPermi('icbc:scanuser:remove')")
    @Log(title = "扫码人员管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{scanUserIds}")
    public AjaxResult remove(@PathVariable Long[] scanUserIds) {
        return toAjax(scanUserService.deleteScanUserById(scanUserIds));
    }

}
